<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/A_19JO/mao.git/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/A_19JO/mao.git/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/A_19JO/mao.git/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/A_19JO/mao.git/images/logo.svg" color="#222">

<link rel="stylesheet" href="/A_19JO/mao.git/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" integrity="sha256-dABdfBfUoC8vJUBOwGVdm8L9qlMWaHTIfXt+7GnZCIo=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"gitee.com","root":"/A_19JO/mao.git/","images":"/A_19JO/mao.git/images","scheme":"Gemini","darkmode":false,"version":"8.22.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":false,"style":null},"fold":{"enable":false,"height":500},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/A_19JO/mao.git/js/config.js"></script>

    <meta name="description" content="EECSer">
<meta property="og:type" content="website">
<meta property="og:title" content="19JO&#39;s blog">
<meta property="og:url" content="https://gitee.com/A_19JO/mao.git/index.html">
<meta property="og:site_name" content="19JO&#39;s blog">
<meta property="og:description" content="EECSer">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="19JO">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gitee.com/A_19JO/mao.git/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":true,"isPost":false,"lang":"en","comments":"","permalink":"","path":"index.html","title":""}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>19JO's blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/A_19JO/mao.git/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/A_19JO/mao.git/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">19JO's blog</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Hexo Blog</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="Search" role="button">
    </div>
  </div>
</div>







</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">19JO</p>
  <div class="site-description" itemprop="description">EECSer</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/A_19JO/mao.git/archives/">
          <span class="site-state-item-count">2</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">tags</span>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/A_19JO/mao.git/2025/03/16/python-doctest/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/A_19JO/mao.git/images/avatar.gif">
      <meta itemprop="name" content="19JO">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="19JO's blog">
      <meta itemprop="description" content="EECSer">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | 19JO's blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/A_19JO/mao.git/2025/03/16/python-doctest/" class="post-title-link" itemprop="url">python-doctest</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>
      

      <time title="Created: 2025-03-16 18:38:01 / Modified: 19:34:50" itemprop="dateCreated datePublished" datetime="2025-03-16T18:38:01+08:00">2025-03-16</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="python测试工具——doctest"><a href="#python测试工具——doctest" class="headerlink" title="python测试工具——doctest"></a>python测试工具——doctest</h1><h2 id="brief-introduction"><a href="#brief-introduction" class="headerlink" title="brief introduction"></a>brief introduction</h2><p><a target="_blank" rel="noopener" href="https://docs.python.org/zh-cn/3/library/doctest.html">doctest</a>是python自带的一个调试测试工具，可用于初步检测你写的python程序的输入与输出结果是否符合你的预期<br>通过在自己写的函数内，按照规定的格式写上你想要对比的输入输出结果，再使用<code>python3 -m doctest example.py</code>(example.py是你要test的python程序)，python会自动帮你用你的输入和你写的程序能否得到你给定的输出</p>
<blockquote>
<p>   python3是linux里运行python的指令<br>    在Windows中应该是python -m doctest example.py</p>
</blockquote>
<blockquote>
<p>   注：本次测试实验我是在linux系统上测试的，可能与windows操作的情况略有不同，如有操作和你的系统不太一样，可尝试搜索windows系统的操作方法</p>
</blockquote>
<p>下面演示一个最直接简单的用法</p>
<h2 id="example"><a href="#example" class="headerlink" title="example"></a>example</h2><p>我们写一个简单的3个数求和的python函数，这里我的python文件名是pydoctest.py</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">add_three_num</span>(<span class="params">a, b, c</span>):</span><br><span class="line">    <span class="keyword">return</span> (a + b + c)</span><br></pre></td></tr></table></figure>
<p>现在python运行一下，看看python显示的样子<br>可以使用python交互式运行指令<code>python3 -i pydoctest.py</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -i pydoctest.py </span><br><span class="line">&gt;&gt;&gt; add_three_num(1,1,1)</span><br><span class="line">3</span><br><span class="line">&gt;&gt;&gt; add_three_num(2,2,2)</span><br><span class="line">6</span><br><span class="line">&gt;&gt;&gt; add_three_num(1,2,3)</span><br><span class="line">6</span><br><span class="line">&gt;&gt;&gt; </span><br></pre></td></tr></table></figure>
<p>注意python IDE或者你的cli界面的显示样式：<code>&gt;&gt;&gt;</code>后有一个空格，直接return的结果前面没有空格，python交互界面显示的样子，就是你要写给doctest的样子，要完全一致，doctest会匹配检查每个字符，只要有一个字符不一致就判定为fail</p>
<p>现在参考<a target="_blank" rel="noopener" href="https://docs.python.org/zh-cn/3/library/doctest.html">doctest</a>文档，在函数内写上你想测试的内容</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">add_three_num</span>(<span class="params">a, b, c</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    &gt;&gt;&gt; add_three_num(1,2,3)</span></span><br><span class="line"><span class="string">    6</span></span><br><span class="line"><span class="string">    </span></span><br><span class="line"><span class="string">    &gt;&gt;&gt; add_three_num(3,2,3)</span></span><br><span class="line"><span class="string">    8</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> (a + b + c)</span><br></pre></td></tr></table></figure>
<p>现在我们使用doctest来看看效果</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -m doctest pydoctest.py </span><br><span class="line">$ </span><br></pre></td></tr></table></figure>
<p>没有任何输出，因为测试pass，不会显示任何信息，可以在脚本命令加上-v来显示所有信息</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -m doctest pydoctest.py -v</span><br></pre></td></tr></table></figure>
<p>输出结果为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">Trying:</span><br><span class="line">   add_three_num(1,2,3)</span><br><span class="line">Expecting:</span><br><span class="line">   6</span><br><span class="line">ok</span><br><span class="line">Trying:</span><br><span class="line">   add_three_num(3,2,3)</span><br><span class="line">Expecting:</span><br><span class="line">   8</span><br><span class="line">ok</span><br><span class="line">1 items had no tests:</span><br><span class="line">   pydoctest</span><br><span class="line">1 items passed all tests:</span><br><span class="line">  2 tests <span class="keyword">in</span> pydoctest.add_three_num</span><br><span class="line">2 tests <span class="keyword">in</span> 2 items.</span><br><span class="line">2 passed and 0 failed.</span><br><span class="line">Test passed.</span><br></pre></td></tr></table></figure>
<p>如果我们python程序有误，比如我把最后的return内容改为&#96;return (a + b - c)<br>再来运行一下看看</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -m doctest pydoctest.py</span><br><span class="line">**********************************************************************</span><br><span class="line">File <span class="string">&quot;/home/mao/MyBlog/blog/source/_posts/Python-doctest-post/pydoctest.py&quot;</span>, line 3, <span class="keyword">in</span> pydoctest.add_three_num</span><br><span class="line">Failed example:</span><br><span class="line">    add_three_num(1,2,3)</span><br><span class="line">Expected:</span><br><span class="line">    6</span><br><span class="line">Got:</span><br><span class="line">    0</span><br><span class="line">**********************************************************************</span><br><span class="line">File <span class="string">&quot;/home/mao/MyBlog/blog/source/_posts/Python-doctest-post/pydoctest.py&quot;</span>, line 6, <span class="keyword">in</span> pydoctest.add_three_num</span><br><span class="line">Failed example:</span><br><span class="line">    add_three_num(3,2,3)</span><br><span class="line">Expected:</span><br><span class="line">    8</span><br><span class="line">Got:</span><br><span class="line">    2</span><br><span class="line">**********************************************************************</span><br><span class="line">1 items had failures:</span><br><span class="line">   2 of   2 <span class="keyword">in</span> pydoctest.add_three_num</span><br><span class="line">***Test Failed*** 2 failures.</span><br></pre></td></tr></table></figure>
<p>可以看到它会提示你出错的文件，与期望不一致的结果，测试pass、fail了几项等等</p>
<p>如果要doctest的py程序和函数较多，也可以把doctest内容单独编写一个文件，运行doctest.testfile(“example.txt”)来测试，详情可阅读doctest官方文档，有机会的话，我在尝试一下出个blog</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/A_19JO/mao.git/2025/03/16/hello-world/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/A_19JO/mao.git/images/avatar.gif">
      <meta itemprop="name" content="19JO">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="19JO's blog">
      <meta itemprop="description" content="EECSer">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="undefined | 19JO's blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/A_19JO/mao.git/2025/03/16/hello-world/" class="post-title-link" itemprop="url">Hello World</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2025-03-16 17:42:08" itemprop="dateCreated datePublished" datetime="2025-03-16T17:42:08+08:00">2025-03-16</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>Welcome to <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a target="_blank" rel="noopener" href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a target="_blank" rel="noopener" href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">&quot;My New Post&quot;</span></span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>

<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>





</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">19JO</span>
  </div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/1Allen9" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/A_19JO/mao.git/js/comments.js"></script><script src="/A_19JO/mao.git/js/utils.js"></script><script src="/A_19JO/mao.git/js/motion.js"></script><script src="/A_19JO/mao.git/js/sidebar.js"></script><script src="/A_19JO/mao.git/js/next-boot.js"></script><script src="/A_19JO/mao.git/js/bookmark.js"></script>

  






  





</body>
</html>
